package com.graduate.examapp.broadcast

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.graduate.examapp.service.Store
import com.graduate.examapp.service.UserNotice
import com.graduate.examapp.view.activity.AlarmActivity


class AlarmBroadcastReciever: BroadcastReceiver() {
    /**
     *  广播接收者的生命周期非常短暂，在接收到广播的时候创建，onReceive()方法结束之后销毁；
     */
    override fun onReceive(context: Context?, intent: Intent?) {
        Log.d(TAG, "收到广播")


        val intentAlarm = Intent(context, AlarmActivity::class.java)
        val alarmMessage = intent?.getStringExtra("alarm_message")
        intentAlarm.putExtra("alarm_message", alarmMessage)
        //intentAlarm.putExtra("ring_tone", ringtone)
        val pendingIntent = PendingIntent.getActivity(
            context,
            0,
            intentAlarm,
            PendingIntent.FLAG_IMMUTABLE
        )
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val descriptionText = "channel"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(UserNotice.CHANNEL_ID, "exam_task", importance).apply {
                description = descriptionText
            }
            // Register the channel with the system
            val notificationManager: NotificationManager =
                context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
        context?.let {
            val notification = NotificationCompat.Builder(context, UserNotice.CHANNEL_ID)
                .setSmallIcon(com.graduate.examapp.R.drawable.icon_notice_black_32)
                .setContentTitle("ExamApp")
                .setContentText("快要监考啦！点击查看详情")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent) //alarm clock
                .setAutoCancel(true)
                .setDeleteIntent(pendingIntent)
                .setFullScreenIntent(pendingIntent, true)
                .build()
            NotificationManagerCompat.from(context)
                .notify(1, notification)
        }
        val ringtoneUri = Uri.parse("android.resource://" + context?.packageName + "/" +  com.graduate.examapp.R.raw.in_call_alarm)
        val ringtone = RingtoneManager.getRingtone(context, ringtoneUri)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            ringtone.isLooping = true
        }
        Store.ringtone = ringtone
        ringtone.play()
        //val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        //vibrator.vibrate(VibrationEffect.createWaveform(longArrayOf(100, 500, 100, 500), 0))
    }

    companion object {
        const val TAG = "AlarmBroadcastReciever"
    }
}